class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        
        int n = intervals.size();
        if(n == 1) return intervals;

        vector<vector<int>> merge;
        sort(intervals.begin(), intervals.end());
        
        for(int i=0; i<n; i++){

            int left=intervals[i][0], right=intervals[i][1];
            if(!merge.size() || merge.back()[1] < left){
                merge.push_back({left, right});
            }
            else if(merge.back()[1] < right){
                merge.back()[1] = right;
            }
        }

        return merge;
    }
};
};